package com.student.action;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.student.entity.Student;
import com.student.service.StudentService;
import com.student.service.impl.StudentServiceImpl;


@WebServlet("/student/*")
public class StudentAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final int PAGE_COUNT = 3;//每页显示记录数
    private StudentService studentService = new StudentServiceImpl();
    
    public StudentAction() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getPathInfo();
		if("/list".equals(path)) {
			
			//默认当前页
			int currentPage = 1;
			String likename = request.getParameter("likename");
			String page = request.getParameter("page");
			if(page != null) {
				currentPage = Integer.valueOf(page);
			}
			List<Student> list = studentService.findList(currentPage,PAGE_COUNT, likename);
			int totalPage = studentService.getTotalPage(PAGE_COUNT,likename);
			request.setAttribute("list", list);
			request.setAttribute("totalPage", totalPage);
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("likename", likename == null ? "" : likename);
			request.getRequestDispatcher("/WEB-INF/page/studentList.jsp").forward(request, response);
		} else if("/form".equals(path)) {
			String sid = request.getParameter("id");
			if(sid != null) {
				//查询学生
				Student student = studentService.findStudentById(sid);
				request.setAttribute("student", student);
			}
			request.getRequestDispatcher("/WEB-INF/page/studentForm.jsp").forward(request, response);
		} else if("/save".equals(path)) {
			//获取学生数据
			Student student = getStudentDatas(request);
			studentService.saveStudent(student);
			response.sendRedirect(request.getContextPath() + "/student/list");
		} else if("/delete".equals(path)) {
			String sids = request.getParameter("sids");
			studentService.deleteStudent(sids);
			response.sendRedirect(request.getContextPath() + "/student/list");
		}else if ("/showStudent".equals(path)) {
			String sid = request.getParameter("id");
			if(sid != null) {
				//查询学生
				Student student = studentService.findStudentById(sid);
				request.setAttribute("student", student);
			}
			request.getRequestDispatcher("/WEB-INF/page/showStudent.jsp").forward(request, response);
		}
 	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	//封装数据
	private Student getStudentDatas(HttpServletRequest request) {
		String id = request.getParameter("sid");
		String sname = request.getParameter("sname");
		String studentNumber = request.getParameter("studentNumber");
		String birthday = request.getParameter("birthday");
		String sex = request.getParameter("sex");
		String polity = request.getParameter("polity");
		String brief = request.getParameter("brief");
		Student student = new Student();
		if(id != null && id !="") {
			student.setId(Integer.valueOf(id));
			student.setNewRecord(false);
		}else {
			student.setNewRecord(true);
		}
		student.setSname(sname);
		student.setStudentNumber(studentNumber);
		student.setSex(Integer.valueOf(sex));
		student.setPolity(Integer.valueOf(polity));
		student.setBrief(brief);
		try {
			student.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return student;
	}

}
